#!/usr/bin/env node

const com= require('commander');
const rp = require('request-promise');
const md5 = require('md5');
const clc = require('cli-color');

let uri = 'https://fanyi-api.baidu.com/api/trans/vip/translate';
let _obj = {
    q: process.argv[2],
    from: 'auto',
    to: process.argv[3] || 'zh',
    appid: '20181008000216417',
    salt: 1435660288,
    sign: '',
    secret: 'kyinSkdXiJ5Fgpgy_DFf'
}
_obj.sign=md5(_obj.appid + _obj.q + _obj.salt + _obj.secret) 

async function f (){
    try{
        let result = await rp({
            method: 'GET',
            uri: uri,
            qs: _obj,
            json: true
          })
          if(process.argv[4] == 't'){
            let str = result.trans_result[0].dst; 
            let  capitalizeEveryWord = str => str.replace(/\b[a-z]/g, char => char.toUpperCase()) 
            let _str = capitalizeEveryWord(str).toString().split(" "); 
            let res = '';
            for (let i = 0; i < _str.length; i++) {
                if(i == 0){
                    res += _str[i].toLowerCase()
                }else{
                    res += _str[i]
                }
            }
            console.log(clc.whiteBright('小驼峰法输入:'), clc.bgYellow(clc.red(result.trans_result[0].src)))
            console.log(clc.whiteBright('小驼峰法输出:'), clc.bgYellow(clc.red(res)))
        }else{
            console.log(clc.whiteBright('输入:'), clc.bgYellow(clc.red(result.trans_result[0].src)))
            console.log(clc.whiteBright('输出:'), clc.bgYellow(clc.red(result.trans_result[0].dst)))
        }
    }catch(err){
        console.log(err)
    };
}

f(process.argv[2])

com.parse(process.argv);